[#macro couponruleUpdateView]
<div class="model-form">
    <el-page-header @back="goBack" content="更新优惠券">
    </el-page-header>
    <div class="model-content">
        <el-form ref="ruleForm" :rules="rules" :model="form" label-width="160px">
            <el-row :gutter="10">
                <el-col :span="20">
                    <el-form-item label="名称" size="mini" prop="name">
                        <el-input v-model="form.name" name="name">
                        </el-input>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="面额" size="mini" prop="money">
                        <el-input v-model="form.money" name="money">
                            <template slot="append">元</template>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="20">
                    <el-form-item label="封面" size="mini" prop="logo">
                        <avatar v-model="form.logo"></avatar>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="商家" size="mini" prop="shop">
                        <el-select style="width: 100%;" v-model="form.shop" filterable clearable placeholder="请选择">
                            <el-option v-for="item in shopOptions" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="使用门槛" size="mini" prop="minPrice">
                        <div>
                            <el-input v-model.number="form.minPrice" name="minPrice">
                                <template slot="prepend">满</template>
                                <template slot="append">元可用 *填“0”不做限制 </template>
                            </el-input>
                        </div>

                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="每人限领" size="mini" prop="limitNum">
                        <el-input-number v-model="form.limitNum" name="limitNum" type="textarea" :min="0" :max="100">
                        </el-input-number>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="发放时间" size="mini" prop="sendBeginTime">
                        <el-date-picker v-model="sendTime" @change="changeSendTime" type="datetimerange"
                            value-format="yyyy-MM-dd HH:mm" placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="有效期" size="mini" prop="expireType">
                        <el-radio v-model="form.expireType" label="date">固定日期</el-radio>
                        <el-radio v-model="form.expireType" label="day">自领取当天起</el-radio>
                    </el-form-item>
                </el-col>
                <el-col :span="20" v-if="form.expireType=='date'">
                    <el-form-item label="优惠券有效期" size="mini" prop="useBeginTime">
                        <el-date-picker v-model="useTime" @change="changeUseTime" type="datetimerange"
                            value-format="yyyy-MM-dd HH:mm" placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>

                </el-col>
                <el-col :span="20" v-if="form.expireType=='day'">
                    <el-form-item label="固定天数" size="mini" prop="useDay">
                        <el-input-number v-model="form.useDay" name="useDay" type="textarea" :min="1" :max="999">
                        </el-input-number>
                    </el-form-item>

                </el-col>
                <el-col :span="20">
                    <el-form-item label="卡券库存" size="mini" prop="stock">
                        <el-input-number v-model="form.stock" name="stock" type="textarea" :min="1" :max="100000">
                            <template slot="append">张 </template>
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="20">
                    <el-form-item label="结算费率" size="mini" prop="takeawayRate">
                        <el-input-number v-model="form.takeawayRate" name="takeawayRate"  :min="0" :max="100">
                            <template slot="append">张 </template>
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="20">
                    <el-form-item label="使用说明" size="mini" prop="note">
                        <el-input v-model="form.note" name="note" type="textarea"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24" style="text-align: right;padding-bottom: 20px;padding-top: 20px;">
                    <el-button @click="goBack">取消</el-button>
                    <el-button type="primary" @click="updateData">确定</el-button>
                </el-col>
            </el-row>
        </el-form>
    </div>
</div>
[/#macro]

[#macro couponruleUpdateConfig]
<script type="text/x-template" id="my-couponrule-update">
    [@couponruleUpdateView /]
</script>
<script type="text/javascript">
    //更新组件开始
    var couponruleUpdateConfig = {};
    couponruleUpdateConfig.template = "#my-couponrule-update";
    couponruleUpdateConfig.methods = config.basicMethod();
    couponruleUpdateConfig.methods.updateData = function () {
        var self = this;
        this.$refs["ruleForm"].validate(function (valid) {
            if (valid) {
                self.updateDataPost();
            } else {
                console.log('error submit!!');
                return false;
            }
        });
    }

    couponruleUpdateConfig.methods.changeSendTime = function (event) {
        this.form.sendTime = event;
        if (event == null) {
            this.sendTime= [];
            this.form.sendBeginTime = "";
            this.form.sendEndTime = "";
        } else {
            this.sendTime = event;
            this.form.sendBeginTime = event[0];
            this.form.sendEndTime = event[1];
        }
    }
    couponruleUpdateConfig.methods.changeUseTime = function (event) {
        this.form.useTime = event;

        if (event == null) {
            this.useTime= [];
            this.form.useBeginTime = "";
            this.form.enduseEndTimeTime = "";
        } else {
            this.useTime = event;
            this.form.useBeginTime = event[0];
            this.form.enduseEndTimeTime = event[1];
        }
    }

    couponruleUpdateConfig.methods.updateDataPost = function () {
        var self = this, data = this.form;
        //delete data.couponruleCatalog;
        if (this.deforeUpdateData) {
            this.deforeUpdateData();
        }
        this.postData("${siteurl}tenantRest/couponrule/update.htm", data, function (res) {
            if (res.code == 0) {
                self.$message({
                    message: '修改数据成功',
                    type: 'success'
                });
                //window.history.go(-1)
                self.$router.go(-1);
            } else {
                self.$message.error(res.msg);
            }
        });
    }
    couponruleUpdateConfig.data = function () {
        return {
            form: {
                name: '',
                logo: '',
                money: '',
                shop: '',
                minPrice: '',
                limitNum: '',
                sendBeginTime: '',
                sendEndTime: '',
                expireType: '',
                useBeginTime: '',
                useEndTime: '',
                useDay: '',
                stock: '',
                note: '',
                couponScope: ''
            },
            useTime: [],
            sendTime: [],
            rules: {
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' }
                ],
                money: [
                    { required: true, message: '请输入面额', trigger: 'blur' }
                ],
                shop: [
                    { required: true, message: '请输入商家', trigger: 'blur' }
                ],
                sendBeginTime: [
                    { required: true, message: '请选择时间', trigger: 'blur' }
                ],
                useBeginTime: [
                    { required: true, message: '请选择时间', trigger: 'blur' }
                ]
            }
        }
    };

    couponruleUpdateConfig.mounted = function () {
        var id = this.$route.query.id;
        var self = this;
        var data = {};
        data.id = id;
        this.postData("${siteurl}tenantRest/couponrule/view.htm", data, function (res) {
            if (res.code == 0) {
                self.form = res;
                if (res.sendBeginTime) {
                    self.sendTime = [res.sendBeginTime, res.sendEndTime];
                } else {
                    self.sendTime = [];
                }

                if (res.useBeginTime) {
                    self.useTime = [res.useBeginTime, res.useEndTime];

                } else {
                    self.useTime = [];
                }
            }
        });
    }
    couponruleUpdateConfig.computed = {};
    couponruleUpdateConfig.computed.shopOptions = function () {
        return this.$store.state.shopOptions;
    };

    couponruleUpdateConfig.destroyed = function () {
        console.log("destroyed add view ");

    }
    var couponruleUpdateView = Vue.component('update-view', couponruleUpdateConfig);
    //更新组件结束
</script>
[/#macro]